package com.example.demo.lc.str;

import java.util.ArrayList;
import java.util.List;

public class Lc6 {
    public static void main(String[] args) {
        String  s = "PAYPALISHIRING";
        System.out.println(convert(s,3));
    }

    public static String convert(String s, int numRows) {
        if(numRows == 1) return s;
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < Math.min(s.length(),numRows); i++) {
            rows.add(new StringBuilder());
        }
        int currow = 0;
        boolean goDown = false;
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            rows.get(currow).append(aChar);
            if(currow ==0 || currow==numRows-1) goDown = !goDown;
            currow += goDown?1:-1;
        }
        StringBuilder ssb = new StringBuilder();
        for (StringBuilder row : rows) ssb.append(row.toString());
        return ssb.toString();
    }
}
